package com.sample.snl.frontend.SyntaxAnalysis;

import javax.swing.*;
import java.awt.*;

public class NewPanel extends JLabel {
    public static Node Root;

    public void draw(Node root, Graphics g) {
        g.drawRect(root.getX() - 3, root.getY() - 15, (root.getValue().length() + 1) * 8, 20);
        if (!root.hasChild()) {
            g.drawString(root.getValue(), root.getX(), root.getY());
        } else {
            g.drawString("" + root.getValue(), root.getX(), root.getY());
            if (root.getChildren().length != 0) {
                for (int i = 0; i < root.getChildren().length; i++) {
                    if (!root.getChildren()[i].getValue().equals("empty")) {
                        g.drawLine(root.getX() + root.getValue().length() / 2, root.getY() + 5, root.getChildren()[i].getX() + root.getChildren()[i].getLength() / 2, root.getChildren()[i].getY() - 15);
                        draw(root.getChildren()[i], g);
                    }
                }
            }
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.white);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.black);
        draw(Root, g);
    }
}
